package org.evil.deus.rss.widget;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class RSSWidgetProvider extends ContentProvider{
	/*default*/ static final String AUTHORITY = "org.evil.deus.rss.widget";
	
	private static final int MATCH_ITEMS = 0;
	private static final int MATCH_ITEMS_ID = 1;
	
	private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		MATCHER.addURI(AUTHORITY, RSSWidget.Widget.TABLE, MATCH_ITEMS);
		MATCHER.addURI(AUTHORITY, RSSWidget.Widget.TABLE + "/*", MATCH_ITEMS_ID);
	}
	
	private RSSWidgetHelper helper = null;
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = helper.getWritableDatabase();
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSSWidget.Widget._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			default:
				throw new UnsupportedOperationException();
		}
		int result = db.delete(RSSWidget.Widget.TABLE, selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return result;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Uri result = null;
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				long id = db.insert(RSSWidget.Widget.TABLE, RSSWidget.Widget.ID, values);
				result = id != -1 ? Uri.withAppendedPath(RSSWidget.Widget.CONTENT_URI, String.valueOf(id)) : null;
				getContext().getContentResolver().notifyChange(uri, null);
				break;
			default:
				throw new UnsupportedOperationException();
		}
		return result;
	}

	@Override
	public boolean onCreate() {
		helper = new RSSWidgetHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = helper.getReadableDatabase();
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSSWidget.Widget._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			default:
				throw new UnsupportedOperationException();
		}
		Cursor cursor = db.query(RSSWidget.Widget.TABLE, 
								 null, 
								 selection, 
								 selectionArgs, 
								 null, 
								 null, 
								 null);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = helper.getWritableDatabase();
		switch (MATCHER.match(uri)) {
			case MATCH_ITEMS:
				break;
			case MATCH_ITEMS_ID:
				String _id = uri.getLastPathSegment();
				selection = RSSWidget.Widget._ID + " = ?";
				selectionArgs = new String [] {_id};
				break;
			default:
				throw new UnsupportedOperationException();
		}
		int result = db.update(RSSWidget.Widget.TABLE, values, selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return result;
	}
	
}
